package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.itheima.ydd.entity.Cate;

import com.itheima.ydd.entity.YddArticle;
import com.itheima.ydd.exception.ConsumerException;
import com.itheima.ydd.service.CateService;
import com.itheima.ydd.mapper.CateMapper;
import com.itheima.ydd.service.YddArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CateServiceImpl extends ServiceImpl<CateMapper, Cate>
    implements CateService{

    @Autowired
    private YddArticleService articleService;

    @Override
    public void removeIfNoArticle(Integer id) {

        LambdaQueryWrapper<YddArticle> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(YddArticle::getCateid, id);
        if (articleService.count(queryWrapper) > 0) {
            throw new ConsumerException("该分类下有文章无法删除");
        }
        super.removeById(id);
    }

}




